package com.ruoyi.travel.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.travel.service.IGuideDetailService;
import com.ruoyi.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private IGuideDetailService iguideDetailService;
    @PostMapping()
    public AjaxResult uploadImages(MultipartFile file,Long guideId) {
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   xxx.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;
            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            iguideDetailService.addPath(guideId,filePath);
            return AjaxResult.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }
        return AjaxResult.error("上传失败！");
    }
}
